package com.atguigu.mapreduce.demo.writable;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/18 18:32
 * @description:
 */
public class PhoneCounterPojo implements Writable, Comparable {

    private long upFlow;
    private long downFlow;
    private long sumFlow;


    public PhoneCounterPojo() {
//        super();
    }


    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    public void setSumFlow() {
        this.sumFlow = this.upFlow + this.downFlow;
    }


    @Override
    public int compareTo(Object o) {

        if (o instanceof PhoneCounterPojo) {
            PhoneCounterPojo p = ((PhoneCounterPojo) o);
            return Integer.parseInt((this.sumFlow - p.sumFlow) + "");
        }

        return 0;
    }


    /**
     * 序列化
     *
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {

        //这个一旦确定下来, 反序列化要与之保持一致
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(sumFlow);
    }

    /**
     * 反序列化
     *
     * @param in
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.upFlow = in.readLong();
        this.downFlow = in.readLong();
        this.sumFlow = in.readLong();
    }


    @Override
    public String toString() {
        return this.upFlow + "\t" + this.downFlow + "\t" + this.sumFlow;
    }
}

